import java.util.ArrayList;

public class Curve {
    private final ArrayList<Student> studentList;
    private double startScore;  // 这个有什么用途呢？
    private double scale;

    public Curve(double sScore, double full) {
        studentList = new ArrayList<Student>();
        startScore = sScore;
        scale = full;
    }

    public void initialize() {
        for (Student s : studentList) {
            s.setCurvedScore(0.0);
        }
    }

    public ArrayList<Student> getStudentList() {
        return studentList;
    }

    public void addStudent(Student s) {
        studentList.add(s);
    }

    public double getStartScore() {
        return startScore;
    }

    public void setStartScore(double s1) {
        startScore = s1;
    }

    public double getScale() {
        return scale;
    }

    public void setScale(double s2) {
        scale = s2;
    }

    //算平均数
    public double Average() {
        double sum = 0;
        for (Student student : studentList) {
            sum += student.getOriginalScore();
        }
        double avg = sum / studentList.size();
        return avg;
    }

    //算标准差
    public double standard() {
        double avg = Average();
        double sum = 0;
        for (Student student : studentList) {
            sum += (student.getOriginalScore() - avg) * (student.getOriginalScore() - avg);
        }
        double sampleStd = Math.sqrt(sum / (studentList.size() - 1));  // 百度百科有两种计算标准差的公式
        double overallStd = Math.sqrt(sum / studentList.size());
        return overallStd;
    }

    //算z-score
    public void zCurve() {
        double avg = Average();
        double std = standard();
        for (Student student : studentList) {
            double curvedScore = (student.getOriginalScore() - avg) / std;
            student.setCurvedScore(curvedScore);
        }
    }

    //算整体curve
    public void bndsCurve() {
        for (Student student : studentList) {
            double curvedScore = Math.sqrt(student.getOriginalScore()) * scale;
            student.setCurvedScore(curvedScore);
        }
    }

    public String checkStudent(String name) {
        String result = "";
        Student s = null;
        for (Student k : studentList) {
            if (k.getName().equals(name)) {
                s = k;
                break;
            }
        }
        result = "Name: " + s.getName() + "\nOriginal Score: " + s.getOriginalScore()
                + "\nCurved Score: " + s.getCurvedScore();
        return result;
    }

    public String toString() {
        String result = "Name\t\tOriginal Score\t\tCurved Score\n";
        for (Student s : studentList) {
            result += s.toString();
        }
        return result;
    }
}
